import { http } from '@/utils/request'
import type {
  IResAssignList,
  IResUserList,
  ISaveUserParams,
  IUpdateUserParams
} from '../types/userList'
import _ from 'lodash'
import qs from 'qs'

/**
 * @description 获取用户列表
 * @param page 当前页码
 * @param limit 每页请求条数
 * @param username 用户名字
 * @returns
 */
export const reqUserList = (page: number, limit: number, username: string) => {
  return http<IResUserList>(`/admin/acl/user/${page}/${limit}`, 'GET', _.pickBy({ username }))
}

/**
 * @description 新增用户
 * @param params
 * @returns
 */
export const reqSaveUser = (params: ISaveUserParams) => {
  return http('/admin/acl/user/save', 'POST', params)
}

/**
 * @description 删除用户
 * @param id 用户 id
 * @returns
 */
export const reqDelUser = (id: number) => {
  return http(`/admin/acl/user/remove/${id}`, 'DELETE')
}

/**
 * @description 更新用户信息
 * @param params
 * @returns
 */
export const reqUpdateUser = (params: IUpdateUserParams) => {
  return http('/admin/acl/user/update', 'PUT', params)
}

/**
 * @description 获取当前用户的角色列表
 * @param id 用户id
 * @returns
 */
export const reqAssignList = (id: number) => {
  return http<IResAssignList>(`/admin/acl/user/toAssign/${id}`, 'GET')
}

/**
 * @description 分配角色
 * @param params
 * @returns
 */
export const reqSetAssign = (userId: string, roleId: string) => {
  return http(
    '/admin/acl/user/doAssign',
    'POST',
    qs.stringify({
      userId,
      roleId
    })
  )
}
